using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using MyNetwork;
using TMPro;
using Photon.Pun;
using Photon.Realtime;

public class MainUI : Singleton<MainUI>
{
    public Button connectBtn;
    public TMP_InputField nameInput;
    const string playerNamePrefKey = "PlayerName";
    public Launcher launcher;
    public GameObject startPanel;
    public GameObject waittingPanel;
    public Button[] roomBtns;

    protected override void Awake()
    {
        base.Awake();

        connectBtn.onClick.AddListener(ConnectLobby);

        foreach (Button btn in roomBtns)
        {
            btn.GetComponentInChildren<Text>().text = "0/" + launcher.maxPlayersPerRoom;
            btn.onClick.AddListener(delegate ()
            {
                LoadLevelScene(btn.transform.GetSiblingIndex());
            });
        }
    }
    private void Start()
    {
        if (!PhotonNetwork.InRoom)
        {
            // 显示标题和玩家名称输入框
            ShowWaittingUI(false);
            // 读取默认玩家名称
            InitPlayerName();
        }
        // 从游戏场景中退出房间，进入了默认房间
        else
        {
            ShowLevelUI();
        }
    }
    // 输入玩家名称后，连接服务器，进入大厅
    private void ConnectLobby()
    {
        // 验证玩家名称的长度
        if (nameInput.text.Length > 2)
        {
            PhotonNetwork.NickName = nameInput.text;
            PlayerPrefs.SetString(playerNamePrefKey, nameInput.text);

            if (!PhotonNetwork.IsConnected)
            {
                PhotonNetwork.ConnectUsingSettings();
                ShowWaittingUI(true);
            }
        }
    }
    public void ShowWaittingUI(bool ifConnected)
    {
        startPanel.SetActive(!ifConnected);
        waittingPanel.SetActive(ifConnected);
    }
    public void ShowLevelUI()
    {
        // 关闭提示信息
        waittingPanel.SetActive(false);
        // 关闭标题页 TitleBox
        transform.GetChild(0).gameObject.SetActive(false);
        // 开启选择关卡页 LevelBox
        transform.GetChild(1).gameObject.SetActive(true);
    }
    private void LoadLevelScene(int i)
    {
        if (i == launcher.roomIndex)
        {
            return;
        }
        string roomName = "Room" + i.ToString();
        Debug.Log(PhotonNetwork.InLobby + "--" + PhotonNetwork.InRoom + "--" + roomName);
        if (PhotonNetwork.InLobby)
        {
            launcher.roomIndex = i;
            launcher.JoinOrCreateRoom(roomName, launcher.maxPlayersPerRoom);
        }
        else if (PhotonNetwork.InRoom)
        {
            // 设置新的房间号
            launcher.roomIndex = i;
            // 离开房间
            launcher.LeaveRoom();
            // 在离开房间的回调中，有新的房间号就会自动加入新房间
        }
    }
    private void InitPlayerName()
    {
        string defaultName = string.Empty;
        if (nameInput != null && PlayerPrefs.HasKey(playerNamePrefKey))
        {
            defaultName = PlayerPrefs.GetString(playerNamePrefKey);
            nameInput.text = defaultName;
        }
        PhotonNetwork.NickName = defaultName;
    }
}
